<?php
// +----------------------------------------------------------------------
// | 零云 [ 简单 高效 卓越 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.lingyun.net All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Model;

use Common\Model\ModelModel;

/**
 * 文章模型
 * @author jry <598821125@qq.com>
 */
class BrandModel extends ModelModel
{
    /**
     * 数据库真实表名
     * 一般为了数据库的整洁，同时又不影响Model和Controller的名称
     * 我们约定每个模块的数据表都加上相同的前缀，比如微信模块用weixin作为数据表前缀
     * @author jry <598821125@qq.com>
     */
    protected $tableName = 'admin_brand';

    /**
     * 自动验证规则
     * @author jry <598821125@qq.com>
     */
    protected $_validate = array(
        array('brand_name', 'require', '品牌名不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('brand_txt', 'require', '品牌介绍不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('position', 'require', '定位不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('slogan', 'require', 'slogan不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('client_txt', 'require', '品牌消费者不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        //array('title', '', '标题已经存在', self::VALUE_VALIDATE, 'unique', self::MODEL_BOTH),
    );

    /**
     * 自动完成规则
     * @author jry <598821125@qq.com>
     */
    protected $_auto = array(
        array('create_time', 'time', self::MODEL_INSERT, 'function'),
        array('update_time', 'time', self::MODEL_BOTH, 'function'),
        array('status', '1', self::MODEL_INSERT),
    );

    /**
     * 查找后置操作
     * @author jry <598821125@qq.com>
     */
    protected function _after_find(&$result, $options)
    {
        

         $result['kv_url'] = get_cover($result['kv'], 'default');
         $result['brand_upload_url'] = $this->multi_upload($result['brand_upload']);
         $result['client_upload_url'] = $this->multi_upload($result['client_upload']);
         $result['product_info_upload_url'] = $this->multi_upload($result['product_info_upload']);
         $result['brand_user'] = D('Admin/User')->getUserInfo($result['brand_id'],'nickname');


    }

    private function multi_upload($ids) {
        $temp = explode(',', $ids);
        foreach ($temp as $value) {
            $t_upload = D('Admin/Upload')->getUploadInfo($value);
            $url = U(MODULE_NAME . '/Upload/download',array('token' => $t_upload['download_token']));
            $data.= "<a href='".$url."'>".$t_upload["name"]."</a><br>";
            //$data[] = D('Admin/Upload')->getUploadInfo($value);    
        }
        return $data;
    }

    /**
     * 查找后置操作
     * @author jry <598821125@qq.com>
     */
    protected function _after_select(&$result, $options)
    {
        foreach ($result as &$record) {
            $this->_after_find($record, $options);
        }
    }

    /**
     * 获取列表
     * @author jry <598821125@qq.com>
     */
    public function getList($cid, $limit = 10, $page = 1, $order = null, $map = null)
    {
        $con["status"] = array("eq", '1');
        if ($map) {
            $map = array_merge($con, $map);
        }
        if (!$order) {
            $order = 'id desc';
        }
        $list = $this->page($page, $limit)
            ->order($order)
            ->where($map)
            ->select();

        return $list;
    }
}
